/**
 * 
 */
package fr.cephb.socialsnp.webapp.form;

import java.util.regex.Pattern;

import fr.cephb.util.MessageType;

/**
 * @author lindenb
 *
 */
public class RegexValidator
	extends AbstractValidator<Pattern>
	{
	public RegexValidator(Pattern pattern)
		{
		super(pattern);
		}
	public RegexValidator(String pattern,int flags)
		{
		this(Pattern.compile(pattern,flags));
		}
	
	public RegexValidator(String pattern)
		{
		this(pattern,0);
		}
	
	/* (non-Javadoc)
	 * @see fr.cephb.socialsnp.webapp.form.Validator#validate(fr.cephb.socialsnp.webapp.form.FormParam)
	 */
	@Override
	public void validate(FormParam param)
		{
		for(String s:param.getValues())
			{
			if(!getValue().matcher(s.trim()).matches())
				{
				param.getErrors().add(new ErrorMessage(
						MessageType.ERROR,
						param.getLabel()+" should match "+getValue().pattern()
						));
				}
			}
		}

}
